package sign_in.loginsystem.service.realize;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sign_in.loginsystem.mapper.DepartmentMapper;
import sign_in.loginsystem.mapper.UserMapper;
import sign_in.loginsystem.model.util.PageBean;
import sign_in.loginsystem.model.util.RBody;
import sign_in.loginsystem.model.entity.User;
import sign_in.loginsystem.service.port.AdminService;

import java.util.ArrayList;


@Service
public class AdministerService implements AdminService {

    @Resource
    UserMapper userMapper;

    @Resource
    DepartmentMapper departmentMapper;


    @Override
    public PageBean allUser(RBody body) {
        PageBean pageBean = body.getPageBean();
        PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        ArrayList<User> user = userMapper.allUser();
        for (User value : user) {
            Integer department = value.getDepartment();
            value.setDepartmentName(departmentMapper.searchDepartmentById(department).getDepartmentName());
        }
        return setUserPage(pageBean, (Page<User>) user);
    }

    @Override
    public PageBean editUser(RBody body) {
        departmentMapper.editDepartmentUser(body.getUser());
        userMapper.edit(body.getUser());
        return allUser(body);
    }

    @Override
    public PageBean delUser(RBody body) {
        departmentMapper.deleteDepartmentUser(body.getUser());
        userMapper.administerDel(body.getUser().getId());
        return allUser(body);
    }

    @Override
    public PageBean searchUser(RBody body) {
        PageBean pageBean = body.getPageBean();
        PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        ArrayList<User> user = userMapper.adminSearch(body.getSearch());
        for (User value : user) {
            Integer department = value.getDepartment();
            value.setDepartmentName(departmentMapper.searchDepartmentById(department).getDepartmentName());
        }
        return setUserPage(pageBean, (Page<User>) user);
    }

    @Transactional
    @Override
    public PageBean createUser(RBody body) {
        User user=body.getUser();
        userMapper.createUser(user);
        userMapper.insertRoleAndUser(user);
        departmentMapper.setUpDepartmentUser(user);
        return allUser(body);
    }

    private PageBean setUserPage(PageBean pageBean, Page<User> user) {
        pageBean.setAllObjectNum((int) user.getTotal());
        pageBean.setPageUser((ArrayList<User>) user.getResult());
        return getPageBean(pageBean);
    }

    private PageBean getPageBean(PageBean pageBean) {
        pageBean.setAllObjectNum(userMapper.userNum());
        if(userMapper.userNum()%pageBean.getPageSize()!=0){
            pageBean.setAllPageNum(userMapper.userNum()/pageBean.getPageSize()+1);
        }
        else {
            pageBean.setAllPageNum(userMapper.userNum()/pageBean.getPageSize());
        }
        return pageBean;
    }



}
